﻿using System;
using System.Web.Mvc;
using System.Text;
using System.IO;
using Newtonsoft.Json;
using AFI.Speargun.Web.Models;
using System.Data.Objects.DataClasses;
using AFI.Speargun.Web.Core;

namespace AFI.Speargun.Web.Helpers
{
    public static class JsonHelper
    {
        public static string GetValidationSummary(this ModelStateDictionary msd)
        {
            StringBuilder sb = new StringBuilder();
            TagBuilder ul = new TagBuilder("ul");

            foreach (ModelState state in msd.Values)
            {
                foreach (ModelError error in state.Errors)
                {
                    string errorMessage = error.ErrorMessage;
                    if (!String.IsNullOrEmpty(errorMessage))
                    {
                        TagBuilder li = new TagBuilder("li");
                        li.SetInnerText(errorMessage);
                        sb.AppendLine(li.ToString(TagRenderMode.Normal));
                    }
                }
            }
            ul.InnerHtml = sb.ToString();
            return (ul.ToString(TagRenderMode.Normal));
        }

        public static string ToJson<T>(this IHierarchical<T> tree) where T : EntityObject, IHierarchical<T>
        {
            StringBuilder sb = new StringBuilder();
            using (StringWriter sw = new StringWriter(sb))
            {
                using (JsonWriter jsonWriter = new JsonTextWriter(sw))
                {
                    jsonWriter.Formatting = Formatting.Indented;
                    JsonHelper.JsonBuilder(tree, jsonWriter);
                }
            }

            return sb.ToString();
        }

        private static void JsonBuilder<T>(IHierarchical<T> tree, JsonWriter jsonWriter) where T : EntityObject, IHierarchical<T>
        {
            /*
             * {
             *     attributes: { id : "node_identificator", [attribute : "attribute_value"] },
             *     state: "closed" or "open",
             *     data: "node_title",
             *     children: [ // an array of child nodes objects ]
             * }
             */
            jsonWriter.WriteStartObject();

            // attributes :
            jsonWriter.WritePropertyName("attributes");
            jsonWriter.WriteStartObject();
            // {
            jsonWriter.WritePropertyName("id");
            jsonWriter.WriteValue(tree.Id.ToString());
            // }
            jsonWriter.WriteEndObject();

            // data :
            jsonWriter.WritePropertyName("data");
            jsonWriter.WriteValue(tree.Appellation);

            tree.Children.Load();
            if (tree.Children.Count != 0)
            {

                // state :
                jsonWriter.WritePropertyName("state");
                jsonWriter.WriteValue("open");

                // children :
                jsonWriter.WritePropertyName("children");
                jsonWriter.WriteStartArray();
                // [
                foreach (var i in tree.Children)
                {
                    if (tree.Id != i.Id)
                        JsonHelper.JsonBuilder(i, jsonWriter);
                }
                // ]
                jsonWriter.WriteEndArray();
            }

            jsonWriter.WriteEndObject();
        }
    }
}
